import { SendSms } from '../../../api/sms.js';
import { Login } from '../../../api/login.js';
import { setToken } from '../../../api/request.js';

const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    userType: null,
    mobile: null,
    smsCode: null,
    smsButtonDisabled: false,
    smsButtonText: '获取验证码',
    smsButtonWaitTime: 0,
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      userType: options.userType
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },
  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  bindMobile(e) {
    this.setData({
      mobile: e.detail.value
    });
  },
  bindSmsCode(e) {
    this.setData({ smsCode: e.detail.value });
  },

  disableSmsButton() {
    this.data.smsButtonWaitTime = 60;
    this.setData({ smsButtonDisabled: true, smsButtonText: this.data.smsButtonWaitTime.toString() });

    const id = setInterval(() => {
      this.data.smsButtonWaitTime--;
      this.setData({ smsButtonText: this.data.smsButtonWaitTime.toString() });
      if (this.data.smsButtonWaitTime === 0) {
        clearInterval(id);
        this.setData({ smsButtonText: '获取验证码', smsButtonDisabled: false });
      }
    }, 1000);
  },
  getSms(e) {
    if (this.data.mobile === null || this.data.mobile.length != 11) {
      wx.showToast({ title: '请输入正确的手机号', icon: 'none', duration: 2000 })
    }
    SendSms({ mobile: this.data.mobile })
      .then(data => {
        wx.showToast({ title: '验证码发送成功', icon: 'none', duration: 2000 });
        this.disableSmsButton();
      })
      .catch(err => {
        const message = '';
        if (err.code === 1001) {
          wx.showToast({ title: '请输入正确的手机号', icon: 'none', duration: 2000 });
        } else if (err.coe === 1002) {
          wx.showToast({ title: '验证码发送过于频繁', icon: 'none', duration: 2000 });
        } else {
          wx.showToast({ title: '验证码发送过于频繁', icon: 'none', duration: 2000 });
        }
      })
  },

  UserLogin(e) {
    if (this.data.smsCode == null || this.data.smsCode.length != 6) {
      wx.showToast({ title: '请输入正确的验证码', icon: 'none', duration: 2000 });
      return;
    }

    console.log(app.globalData);
    Login({ userType: this.data.userType, mobile: this.data.mobile, smsCode: this.data.smsCode, wxOpenId: wx.getStorageSync("wxOpenId"), avatar: app.globalData.userInfo.avatarUrl, nickName: app.globalData.userInfo.nickName })
      .then(resp => {
        app.globalData.token = resp.token;
        app.globalData.currentUserInfo = resp.currentUserInfo;

        setToken(resp.token);
        wx.setStorage({
          key: 'token',
          data: resp.token,
        });
        wx.setStorage({
          key: 'currentUser',
          data: resp.currentUserInfo,
        });

        setTimeout(() => {
          if (resp.userType === 'CUSTOMER') {
            wx.reLaunch({ url: '/pages/user/home/home' });
          } else if (resp.userType === 'MERCHANT') {
            wx.reLaunch({ url: '/pages/merchant/home/home' });
          }
        }, 1000);

        wx.showToast({ title: '登录成功', icon: 'none', duration: 2000 });
      })
      .catch(err => {
        console.log(err);
        wx.showToast({ title: '登录失败', icon: 'none', duration: 2000 });
      });
  }
})